@file:Suppress("SpellCheckingInspection")

package han.cirno.corrupt.components.school.impl.fafu

import han.cirno.corrupt.data.instances.UserExamData.Exam
import han.cirno.corrupt.util.RegexUtil
import org.jsoup.Jsoup

object FafuExamResolver {
    fun analyse(htmlString: String):List<Exam>{
        val document = Jsoup.parse(htmlString)
        val tbody = document.getElementById("DataGrid1")!!.getElementsByTag("tbody")[0]
        val exams:MutableList<Exam> =ArrayList()
        tbody.children().forEach {
            if (it.className()=="datelisthead")return@forEach
            val children = it.children()
            val dateAndTime=children[3].text()
            val date=RegexUtil.matchFirst("\\d.*?(?=\\()",dateAndTime)?:""
            val time=RegexUtil.matchFirst("(?<=\\().*?(?=\\))",dateAndTime)?:""
            val year= RegexUtil.matchFirst("\\d.*?(?=年)",date)?:""
            val month= RegexUtil.matchFirst("(?<=年)\\d.*?(?=月)",date)?:""
            val day= RegexUtil.matchFirst("(?<=月)\\d.*?(?=日)",date)?:""
            val fromAndToTime=time.split("-")
            val fromTime:String
            val fromTimeHourAndMinute:List<String>
            val toTime:String
            val toTimeHourAndMinute:List<String>
            if (fromAndToTime.size>=2){
                fromTime=fromAndToTime[0]
                fromTimeHourAndMinute=fromTime.split(":")
                toTime=fromAndToTime[1]
                toTimeHourAndMinute=toTime.split(":")
            }else{
                fromTimeHourAndMinute= listOf("","")
                toTimeHourAndMinute= listOf("","")
            }
            exams.add(Exam(
                children[1].text(),
                Triple(year,month,day),
                Pair(fromTimeHourAndMinute[0],fromTimeHourAndMinute[1]),
                Pair(toTimeHourAndMinute[0],toTimeHourAndMinute[1]),
                children[7].text()+":"+children[4].text(),
                children[6].text()
            ))
        }
        return exams
    }
}